package com.ajk.server.utils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.env.Environment;
import org.springframework.web.multipart.MultipartFile;

import com.ajk.server.model.DictDiseaseKey;
import com.ajk.server.model.FriendTreatmentPic;
import com.ajk.server.model.Friends;
import com.ajk.server.model.Knowledge;
import com.ajk.server.model.TreatmentPic;
import com.ajk.server.model.Users;
import com.ajk.shop.model.response.SimpleUserProfileRes;


public class FileStoreUtil {
	private final static Logger logger = LoggerFactory.getLogger(FileStoreUtil.class);
	private static Environment env;
	
	public void setEnv(Environment env) {
		FileStoreUtil.env = env;
	}
	
	public static Environment getEnv(){
		return env;
	}
	/**
	 * 获取网络地址 http://www.baidu.com
	 * @return
	 */
	public static String getBaseUrl(){
		String baseUrl = env.getProperty("config.base_url");
		return baseUrl;
	}
	/**
	 * 获取文件存储的目录 /data/www/ajk_res/
	 * @return
	 */
	public static String getExtResRoot() {
		String baseUrl = env.getProperty("ext_res_root");
		return baseUrl;
	}
	
	/**
	 * 存储文件
	 * @param file 上传文件实例
	 * @param storeParentPath 存储的目录
	 * @return
	 */
	public static String storeFile(MultipartFile file,String storeParentPath,Boolean ...notRename){
		
		if (file == null) {   
			return null;   
		}
		long file_size = file.getSize();
		//文件最大10M
		if(file_size>10000000)        
		{   
			throw new RuntimeException("上传失败：文件大小不能超过10M");               
		}
		
		if(!storeParentPath.endsWith("/")){
			storeParentPath = storeParentPath + "/"; 
		}
		
		String absPath = env.getProperty("ext_res_root") + storeParentPath;
		String org_name=file.getOriginalFilename();
		String res_file = null;
		String guuid = AjkUtil.GetGUID();
		if(ArrayUtil.isFlush(notRename)){
			 res_file = guuid +"/"+ org_name;
			 absPath = env.getProperty("ext_res_root") + storeParentPath+ guuid +"/";
		}else{
			 res_file = guuid + AjkUtil.getFileExtension(org_name);
			 absPath = env.getProperty("ext_res_root") + storeParentPath;
		}
		
		
		File dictPath = new File(absPath);
		if( !dictPath.exists()){
			boolean isMkdirs = dictPath.mkdirs();
			if(!isMkdirs){
				throw new RuntimeException("创建文件夹"+absPath+"失败,请确认是否有权限");  
			}
		}
		String dbPath = storeParentPath + res_file;
		String newFileName = env.getProperty("ext_res_root") + dbPath;
		logger.debug("store file path :{}",newFileName);
		logger.debug("dbpath :{}",dbPath);
		File newFile=new File(newFileName);
		newFile.setWritable(true, false);
		try {
			OutputStream os = new FileOutputStream(newFile);
			InputStream is = file.getInputStream();
			byte[] buf = new byte[10240];
			int size = -1;
			while((size = is.read(buf))!=-1){
				os.write(buf,0,size);
			}
			os.flush();
			os.close();
			is.close();
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		dbPath = env.getProperty("ext_res")	+ dbPath;
		return dbPath;
	}
	
	public static void dealWithAvatar(Users user){
		if(!StringUtils.isEmpty(user.getAvatar()) && !StringUtils.startsWithIgnoreCase(user.getAvatar(), "http")){
			user.setAvatar(FileStoreUtil.getBaseUrl()+user.getAvatar());
		}
	}
	public static void dealWithAvatar(Friends user){
		if(!StringUtils.isEmpty(user.getAvatar()) && !StringUtils.startsWithIgnoreCase(user.getAvatar(), "http")){
			user.setAvatar(FileStoreUtil.getBaseUrl()+user.getAvatar());
		}
	}
	public static void dealWithAvatar(SimpleUserProfileRes user){
		if(!StringUtils.isEmpty(user.getAvatar()) && !StringUtils.startsWithIgnoreCase(user.getAvatar(), "http")){
			user.setAvatar(FileStoreUtil.getBaseUrl()+user.getAvatar());
		}
	}
	public static void dealWithTreatmentPic(TreatmentPic treatmentPic){
		if(!StringUtils.isEmpty(treatmentPic.getPath()) && !StringUtils.startsWithIgnoreCase(treatmentPic.getPath(), "http")){
			treatmentPic.setPath(FileStoreUtil.getBaseUrl()+treatmentPic.getPath());
		}
	}
	
	public static void dealWithTreatmentPic(FriendTreatmentPic treatmentPic){
		if(!StringUtils.isEmpty(treatmentPic.getPath()) && !StringUtils.startsWithIgnoreCase(treatmentPic.getPath(), "http")){
			treatmentPic.setPath(FileStoreUtil.getBaseUrl()+treatmentPic.getPath());
		}
	}
	
	public static void dealDictDiseaseKeyIcon(DictDiseaseKey key){
		if(!StringUtils.isEmpty(key.getIcon()) && !StringUtils.startsWithIgnoreCase(key.getIcon(), "http")){
			key.setIcon(FileStoreUtil.getBaseUrl()+key.getIcon());
		}
	}
	
	public static void dealKnowledge(Knowledge knowledge){
		if(!StringUtils.isEmpty(knowledge.getPicUrl()) && !StringUtils.startsWithIgnoreCase(knowledge.getPicUrl(), "http")){
			knowledge.setPicUrl(FileStoreUtil.getBaseUrl()+knowledge.getPicUrl());
		}
	}
}